﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.Text.RegularExpressions;
using WCrawlerLib;
using HtmlAgilityPack;
using System.Threading;
using System.Threading.Tasks;

namespace WCrawler
{
    public partial class frmHostManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string folderId = "2418005";
            

            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();
            //Open host link
            //br.Get("http://fileserve.com/");
            string param = string.Format("loginUserName={0}&loginUserPassword={1}&autoLogin=on&ppp=102&loginFormSubmit=Login",username,password);
            br.PostParams = param;
            br.Post("http://fileserve.com/login.php");
            GridView gridViewPosts = ucPost.GetGridViewControl();
            foreach (GridViewRow r in gridViewPosts.Rows)
            {
                CheckBox chkBx = (CheckBox)r.FindControl("chkCheckItem");
                if (chkBx != null && chkBx.Checked)
                {
                    Label lblHostFolder = (Label)r.FindControl("lblHostFolder");
                    br.PostParams = string.Format("newFolderName={0}&createFolderFormSubmit=Create+folder&folderId=&createFolderDest={1}", lblHostFolder.Text, folderId);
                    br.Post("http://fileserve.com/file-manager.php");
                }
            }
        }

        protected void btnFileFactory_Click(object sender, EventArgs e)
        {
            string email = HttpUtility.UrlEncode(txtEmail.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);

            string parentFolderId = "209b8cf270dd296b"; //Movie Folder

            BrowserSession_FileHost br = new BrowserSession_FileHost();
            //Open host link
            string param = string.Format("redirect=%2F&email={0}&password={1}", email, password);
            br.PostParams = param;
            br.Post("http://filefactory.com/member/login.php",false);
            System.Net.CookieCollection cookieCollect = br.Response.Cookies;

            GridView gridViewPosts = ucPost.GetGridViewControl();
            foreach (GridViewRow r in gridViewPosts.Rows)
            {
                CheckBox chkBx = (CheckBox)r.FindControl("chkCheckItem");
                if (chkBx != null && chkBx.Checked)
                {
                    Label lblHostFolder = (Label)r.FindControl("lblHostFolder");
                    //Add NewFolder in Movie Folder
                    br.PostParams = string.Format("m=addFolder&p={0}",parentFolderId); 
                    br.SaveCookiesFrom(cookieCollect);
                    HtmlDocument doc = br.Post("http://filefactory.com/manager/set.php");

                    //Get created folder id
                    //Template: {status:"ok",data:{attributes:{id:"437ef5169e192c35",mdata:"{}"},data:"New folder"}}
                    Regex re = new Regex("id:\"(?<FolderId>\\w+)\"");
                    Match m = re.Match(doc.DocumentNode.InnerHtml);
                    string folderId = "";
                    if (m.Success)
                    {
                        folderId = m.Result("${FolderId}");
                        //Rename created NewFolder
                        br.PostParams = string.Format("m=editFolder&f={0}&n={1}", folderId, lblHostFolder.Text);
                        br.SaveCookiesFrom(cookieCollect);
                        br.Post("http://filefactory.com/manager/set.php");
                    }
                }
            }
        }

        /// <summary>
        /// Test Post with depositfiles
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDepositfiles_Click(object sender, EventArgs e)
        {
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //Open host link
            br.PostParams = string.Format("go=1&login={0}&password={1}", username, password);
            br.Post("http://depositfiles.com/login.php?return=%2F");

            GridView gridViewPosts = ucPost.GetGridViewControl();
            foreach (GridViewRow r in gridViewPosts.Rows)
            {
                CheckBox chkBx = (CheckBox)r.FindControl("chkCheckItem");
                if (chkBx != null && chkBx.Checked)
                {
                    Label lblHostFolder = (Label)r.FindControl("lblHostFolder");
                    //Add folder
                    br.PostParams = string.Format("action=add_folder&name={0}", lblHostFolder.Text);
                    br.Post("http://depositfiles.com/gold/files_list.php?format=json");
                }
            }

        }

        protected void btnFileSonic_Click(object sender, EventArgs e)
        {
            string url = "";
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);

            string parentFolderId = "D605169"; //D605169 is Movie Folder

            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //Open host link
            br.PostParams = string.Format("email={0}&redirect=%2F&password={1}", username, password);
            //br.Post("http://www.filesonic.vn/user/login");
            br.Post("http://www.filesonic.vn/user/login", false);
            System.Net.CookieCollection cookieCollect = br.Response.Cookies;
            if (br.HtmlDoc.DocumentNode.InnerHtml.Contains("Username") &&
                br.HtmlDoc.DocumentNode.InnerHtml.Contains("Password"))
            {
                txtFolderName.Text = "Login Fail";
                return;
            }
            GridView gridViewPosts = ucPost.GetGridViewControl();
            foreach (GridViewRow r in gridViewPosts.Rows)
            {
                CheckBox chkBx = (CheckBox)r.FindControl("chkCheckItem");
                if (chkBx != null && chkBx.Checked)
                {
                    Label lblHostFolder = (Label)r.FindControl("lblHostFolder");
                    //Add folder
                    br.SaveCookiesFrom(cookieCollect);
                    br.PostParams = string.Format("redirect=%2Ffilesystem%2Fbrowse&id=&name={0}&description=&parent_folder_id={1}&is_public=0&can_access_parent=0", lblHostFolder.Text, parentFolderId);
                    url = string.Format("http://www.filesonic.vn/filesystem/create/{0}", parentFolderId);
                    br.Post(url); 
                }
            }
        }

        protected void btnFileJungle_Click(object sender, EventArgs e)
        {
            string url = "";
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            string folder = HttpUtility.UrlEncode(txtFolderName.Text);

            string parentFolderId = "228587"; //Movie Folder

            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //Open host link
            br.PostParams = string.Format("autoLogin=on&loginUserName={0}&loginUserPassword={1}&loginFormSubmit=", username, password);
            br.Post("http://filejungle.com/login.php", false);
            System.Net.CookieCollection cookieCollect = br.Response.Cookies;
            if (br.HtmlDoc.DocumentNode.InnerHtml.Contains("Username") &&
                br.HtmlDoc.DocumentNode.InnerHtml.Contains("Password"))
            {
                txtFolderName.Text = "Login Fail";
                return;
            }
            GridView gridViewPosts = ucPost.GetGridViewControl();
            foreach (GridViewRow r in gridViewPosts.Rows)
            {
                CheckBox chkBx = (CheckBox)r.FindControl("chkCheckItem");
                if (chkBx != null && chkBx.Checked)
                {
                    Label lblHostFolder = (Label)r.FindControl("lblHostFolder");
                    //Add folder
                    br.SaveCookiesFrom(cookieCollect);
                    br.PostParams = string.Format("newFolderName={0}&createFolderDest={1}", lblHostFolder.Text, parentFolderId);
                    url = "http://filejungle.com/file-manager-action.php";
                    br.Post(url);
                }
            }
        }

        protected void btnUploaded_Click(object sender, EventArgs e)
        {
            string username = HttpUtility.UrlEncode(txtUsername.Text);
            string password = HttpUtility.UrlEncode(txtPassword.Text);
            BrowserSession_FileHost br = new BrowserSession_FileHost();

            //Open host link
            br.PostParams = string.Format("id={0}&pw={1}", username, password);
            br.Post("http://uploaded.to/io/login");

            GridView gridViewPosts = ucPost.GetGridViewControl();
            foreach (GridViewRow row in gridViewPosts.Rows)
            {
                CheckBox chkBx = (CheckBox)row.FindControl("chkCheckItem");
                if (chkBx != null && chkBx.Checked)
                {
                    Label lblHostFolder = (Label)row.FindControl("lblHostFolder");
                    //Add new folder
                    br.Post("http://uploaded.to/io/me/folder/create");

                    string folderId = "";
                    br.Get("http://uploaded.to/me/folder");
                    //http://ul.to/folder/gbjuln/New_folder 
                    Match m = Regex.Match(br.HtmlDoc.DocumentNode.InnerHtml, @"http://ul.to/folder/(?<id>\w+)/New_Folder");
                    if (m.Success)
                    {
                        folderId = m.Result("${id}");
                    }
                    
                    if (folderId == "")
                        return;

                    ////list folders
                    //br.PostParams = "page=0&orderfield=cdate&orderdir=DESC";
                    //HtmlDocument doc = br.Post("http://uploaded.to/io/me/list/folders");

                    ////search newest new folder
                    //string folderId = "";
                    ////{"id":"fddmam","title":"New folder","files":"0","ispublic":false}
                    ////after remove \": {id:fddmam,title:New folder,files:0,ispublic:false}
                    //Regex r = new Regex(@"\{id:(?<id>(.*?)),title:(?<title>(.*?))(.*?)\}");
                    //MatchCollection mCollect = r.Matches(doc.DocumentNode.InnerHtml.Replace("\"",""));
                    //if (mCollect.Count > 0)
                    //{
                    //    Match m = mCollect[0];
                    //    string title = m.Result("${title}");
                    //    folderId = m.Result("${id}");
                    //}

                    //edit name for new folder
                    br.PostParams = string.Format("value={0}&editorId=", lblHostFolder.Text);
                    br.Post(string.Format("http://uploaded.to/io/me/folder/{0}/set/title",folderId));
                }
            }
        }
    }
}